# Network diagnosis script for Docker build issues
# This script helps diagnose and fix network connectivity problems

$ErrorActionPreference = "Stop"

Write-Host "===============================================" -ForegroundColor Cyan
Write-Host "Docker Network Diagnosis Tool" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host ""

# Test 1: Basic internet connectivity
Write-Host "Test 1: Basic Internet Connectivity" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

$testHosts = @("8.8.8.8", "1.1.1.1", "114.114.114.114")
foreach ($host in $testHosts) {
    try {
        $result = Test-NetConnection -ComputerName $host -Port 53 -InformationLevel Quiet -WarningAction SilentlyContinue
        if ($result) {
            Write-Host "✓ $host - Connected" -ForegroundColor Green
        }
        else {
            Write-Host "✗ $host - Failed" -ForegroundColor Red
        }
    }
    catch {
        Write-Host "✗ $host - Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Test 2: DNS resolution
Write-Host "`nTest 2: DNS Resolution" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

$dnsHosts = @("google.com", "github.com", "docker.com")
foreach ($host in $dnsHosts) {
    try {
        $result = Resolve-DnsName -Name $host -ErrorAction Stop
        if ($result) {
            Write-Host "✓ $host - Resolved to $($result.IPAddress)" -ForegroundColor Green
        }
        else {
            Write-Host "✗ $host - Failed to resolve" -ForegroundColor Red
        }
    }
    catch {
        Write-Host "✗ $host - DNS Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Test 3: Docker Hub connectivity
Write-Host "`nTest 3: Docker Hub Connectivity" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

$dockerEndpoints = @(
    "registry-1.docker.io",
    "auth.docker.io",
    "index.docker.io"
)

foreach ($endpoint in $dockerEndpoints) {
    try {
        $result = Test-NetConnection -ComputerName $endpoint -Port 443 -InformationLevel Quiet -WarningAction SilentlyContinue
        if ($result) {
            Write-Host "✓ $endpoint - Connected" -ForegroundColor Green
        }
        else {
            Write-Host "✗ $endpoint - Failed" -ForegroundColor Red
        }
    }
    catch {
        Write-Host "✗ $endpoint - Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Test 4: Alternative registries
Write-Host "`nTest 4: Alternative Registry Connectivity" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

$altRegistries = @(
    "docker.mirrors.ustc.edu.cn",
    "hub-mirror.c.163.com",
    "mirror.baidubce.com"
)

foreach ($registry in $altRegistries) {
    try {
        $result = Test-NetConnection -ComputerName $registry -Port 443 -InformationLevel Quiet -WarningAction SilentlyContinue
        if ($result) {
            Write-Host "✓ $registry - Connected" -ForegroundColor Green
        }
        else {
            Write-Host "✗ $registry - Failed" -ForegroundColor Red
        }
    }
    catch {
        Write-Host "✗ $registry - Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Test 5: Docker daemon status
Write-Host "`nTest 5: Docker Daemon Status" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

try {
    $dockerInfo = docker info 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Docker daemon is running" -ForegroundColor Green
        
        # Check Docker version
        $dockerVersion = docker version --format "{{.Server.Version}}" 2>$null
        if ($dockerVersion) {
            Write-Host "✓ Docker version: $dockerVersion" -ForegroundColor Green
        }
        
        # Check Docker configuration
        $dockerConfigPath = "$env:USERPROFILE\.docker\daemon.json"
        if (Test-Path $dockerConfigPath) {
            Write-Host "✓ Docker config found: $dockerConfigPath" -ForegroundColor Green
            $config = Get-Content $dockerConfigPath | ConvertFrom-Json
            if ($config."registry-mirrors") {
                Write-Host "✓ Registry mirrors configured: $($config.'registry-mirrors'.Count) mirrors" -ForegroundColor Green
            }
        }
        else {
            Write-Host "⚠ No Docker config found" -ForegroundColor Yellow
        }
    }
    else {
        Write-Host "✗ Docker daemon is not running" -ForegroundColor Red
    }
}
catch {
    Write-Host "✗ Docker daemon error: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 6: Proxy settings
Write-Host "`nTest 6: Proxy Settings" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

$proxyVars = @("HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy")
$proxyFound = $false

foreach ($var in $proxyVars) {
    $value = [Environment]::GetEnvironmentVariable($var)
    if ($value) {
        Write-Host "✓ $var = $value" -ForegroundColor Green
        $proxyFound = $true
    }
}

if (-not $proxyFound) {
    Write-Host "⚠ No proxy environment variables found" -ForegroundColor Yellow
}

# Test 7: Common proxy ports
Write-Host "`nTest 7: Common Proxy Ports" -ForegroundColor Green
Write-Host "----------------------------------------" -ForegroundColor Gray

$proxyPorts = @(7890, 8080, 1080, 3128)
foreach ($port in $proxyPorts) {
    try {
        $result = Test-NetConnection -ComputerName "127.0.0.1" -Port $port -InformationLevel Quiet -WarningAction SilentlyContinue
        if ($result) {
            Write-Host "✓ Local proxy on port $port - Available" -ForegroundColor Green
        }
        else {
            Write-Host "✗ Local proxy on port $port - Not available" -ForegroundColor Red
        }
    }
    catch {
        Write-Host "✗ Local proxy on port $port - Error" -ForegroundColor Red
    }
}

# Summary and recommendations
Write-Host "`n===============================================" -ForegroundColor Cyan
Write-Host "DIAGNOSIS SUMMARY" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan

Write-Host "`nRecommended actions:" -ForegroundColor Yellow

Write-Host "1. If Docker Hub connectivity failed:" -ForegroundColor White
Write-Host "   • Run: .\fix-docker-network.ps1" -ForegroundColor Cyan
Write-Host "   • Restart Docker Desktop" -ForegroundColor Cyan
Write-Host "   • Try: .\build-images-offline.ps1" -ForegroundColor Cyan

Write-Host "`n2. If basic internet connectivity failed:" -ForegroundColor White
Write-Host "   • Check your network connection" -ForegroundColor Cyan
Write-Host "   • Try using a different network" -ForegroundColor Cyan
Write-Host "   • Check firewall settings" -ForegroundColor Cyan

Write-Host "`n3. If proxy is detected:" -ForegroundColor White
Write-Host "   • Configure Docker to use your proxy" -ForegroundColor Cyan
Write-Host "   • Or disable proxy for Docker" -ForegroundColor Cyan

Write-Host "`n4. Alternative solutions:" -ForegroundColor White
Write-Host "   • Use VPN" -ForegroundColor Cyan
Write-Host "   • Use mobile hotspot" -ForegroundColor Cyan
Write-Host "   • Try building on a different machine" -ForegroundColor Cyan

Write-Host "`nDiagnosis completed!" -ForegroundColor Green 